﻿using Approval.Domain;
using Contract.Domain;
using Contract.Infrastructure.Repository;
using Contract.WebApi.Application.Commands;
using MediatR;
using Newtonsoft.Json;
using PublicCommon;

namespace Contract.WebApi.Application.CommandHandler
{
    public class AddsCYContractCommandHandler : IRequestHandler<AddsCYContractCommands, int>
    {
        private readonly IRepository<Domain.Contract> _repository;
        private readonly Cross_services_Helper cross_services_helper;
        private readonly IRepository<Tail> repository1;

        public AddsCYContractCommandHandler(IRepository<Domain.Contract> repository, Cross_services_Helper cross_services_helper, IRepository<Tail> repository1)
        {
            _repository = repository;
            this.cross_services_helper = cross_services_helper;
            this.repository1 = repository1;
        }

        public async Task<int> Handle(AddsCYContractCommands request, CancellationToken cancellationToken)
        {
            var info = await _repository.GetAsync(request.ContractId);
            info.ContractTitle = request.ContractTitle;
            info.ShipperEnterprise = request.ShipperEnterprise;
            info.ShipperPrincipal = request.ShipperPrincipal;
            info.Route = request.Route;
            info.TonTransport = request.TonTransport;
            info.Tonnage = request.Tonnage;
            info.CharterAmount = request.CharterAmount;
            info.Responsible = request.Responsible;
            info.SigningDate = request.SigningDate;
            info.ContractAmount = request.ContractAmount;
            info.ContractObject = request.ContractObject;
            info.MainClause = request.MainClause;
            info.ContractText = request.ContractText;
            info.AuditState = request.AuditState;
            info.FreightStatus = request.FreightStatus;
            info.Abnormity = request.Abnormity;
            info.UpdateDate = DateTime.Now;
            var ind = await _repository.UpdateAsync(info);

            var list = await _repository.GetAsync(x => x.ContractNumber == info.ContractNumber);

            var obj = new HCTRelation();
            obj.HCTId = list.ContractNumber;
            obj.ApprovalId = 4;
            obj.ApprovalName = "小明";
            var message = await cross_services_helper.Post("http://10.31.60.9:8000/Approval/api/HCTRelation/HCTRelationAdd", obj);
            if (Convert.ToInt32(message) < 0)
            {
                return await Task.FromResult(-1);
            }

            var res = await cross_services_helper.Get($"http://10.31.60.9:8000/LongLine/api/LineInfo/GetLineRoute");
            var line = JsonConvert.DeserializeObject<List<LineDto>>(res);
            var inds = line.FirstOrDefault(x => x.Id == list.Route);
            Tail tail = new Tail();
            tail.ContractNumber = list.ContractNumber;
            tail.Shipping = inds.Address;
            tail.CreateDate = list.CreateDate;
            tail.ShipperEnterprise = list.ShipperEnterprise;
            tail.Responsible = list.Responsible;
            tail.TrackingState = "合同已创建";
            await repository1.AddAsync(tail);

            return ind;
        }
    }
}
